<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="设备名称" prop="deviceName">
				<el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="探针类型" prop="probeType">
				<el-select v-model="queryParams.probeType" placeholder="请选择探针类型" clearable style="width: 200px">
					<el-option v-for="dict in probe_type" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="绑定类型" prop="bindType">
				<el-select v-model="queryParams.bindType" placeholder="请选择绑定类型" clearable style="width: 200px">
					<el-option v-for="dict in bind_type" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="当前状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择当前状态" clearable style="width: 240px">
					<el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="Plus" @click="handleAdd"
					v-hasPermi="['internet:probe:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
					v-hasPermi="['internet:probe:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
					v-hasPermi="['internet:probe:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport"
					v-hasPermi="['internet:probe:export']">导出</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="probeList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="探针ID" align="center" prop="probeId" />
			<el-table-column label="设备名称" align="center" prop="deviceName" />
			<el-table-column label="探针类型" align="center" prop="probeType">
				<template #default="scope">
					<dict-tag :options="probe_type" :value="scope.row.probeType" />
				</template>
			</el-table-column>
			<el-table-column label="绑定类型" align="center" prop="bindType">
				<template #default="scope">
					<dict-tag :options="bind_type" :value="scope.row.bindType" />
				</template>
			</el-table-column>
			<el-table-column label="端口" align="center" prop="port" />
			<el-table-column label="采集频率" align="center" prop="dataFrequency">
				<template #default="scope">
					<div class="justify-center items-center flex">
						每次/
						<div>{{scope.row.dataFrequency}}</div>
						<dict-tag :options="data_frequency_unit" :value="scope.row.dataFrequencyUnit" />
					</div>
				</template>
			</el-table-column>
			<el-table-column label="状态" align="center" prop="status">
			  <template #default="scope">
				 <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
			  </template>
			</el-table-column>
			<el-table-column label="更新人" align="center" prop="updatedBy" />
			<el-table-column label="更新时间" align="center" prop="updatedDate" width="180">
				<template #default="scope">
					<span>{{ parseTime(scope.row.updatedDate, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" fixed="right" width="130" align="center" class-name="small-padding fixed-width">
				<template #default="scope">
					<el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['internet:probe:edit']">修改</el-button>
					<el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
						v-hasPermi="['internet:probe:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改网络探针信息对话框 -->
		<el-dialog :title="title" v-model="open" width="800px" append-to-body>
			<el-form ref="probeRef" :model="form" :rules="rules" label-width="96px">
				<el-form-item label="探针类型" prop="probeType">
					<el-select v-model="form.probeType" placeholder="请选择探针类型" clearable style="width: 200px">
						<el-option v-for="dict in probe_type" :key="dict.value" :label="dict.label"
							:value="dict.value" />
					</el-select>
				</el-form-item>
				<div class="justify-sb items-center flex">
					<el-form-item label="绑定类型" prop="bindType">
						<el-select v-model="form.bindType" placeholder="请选择绑定类型" clearable
							@change="getDeviceInfo(parseInt(form.bindType))" style="width: 200px">
							<el-option v-for="dict in bind_type" :key="dict.value" :label="dict.label"
								:value="dict.value" />
						</el-select>
					</el-form-item>
					<el-form-item v-if="form.bindType" label="设备信息" prop="deviceId">
						<el-select placeholder="请选择设备信息" clearable @change="onSelectDevice" style="width: 200px">
							<el-option v-for="dict in deviceList" :key="dict.value" :label="dict.label"
								:value="dict.value" />
						</el-select>
					</el-form-item>
				</div>
				<div class="mt-20" v-if="form.bindType">
					<div class="justify-sb items-center flex">
						<el-form-item label="设备ID" prop="deviceId">
							<el-input v-model="form.deviceId" disabled placeholder="请输入请选择设备信息" />
						</el-form-item>
						<el-form-item label="设备名称" prop="deviceName">
							<el-input v-model="form.deviceName" disabled placeholder="请输入请选择设备信息" />
						</el-form-item>
					</div>
				</div>
				<div class="justify-sb items-center flex">
					<el-form-item label="端口" prop="port">
						<el-input v-model="form.port" placeholder="请输入端口" />
					</el-form-item>					
					<el-form-item label="当前状态" prop="status">
						<el-select v-model="form.status" placeholder="请选择当前状态" clearable style="width: 240px">
							<el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label"
								:value="dict.value" />
						</el-select>
					</el-form-item>
				</div>
				<div class="justify-sb items-center flex">
					<el-form-item label="采集频率单位" prop="dataFrequencyUnit">
						<el-select v-model="form.dataFrequencyUnit" placeholder="请选择采集频率单位" clearable
							style="width: 200px">
							<el-option v-for="dict in data_frequency_unit" :key="dict.value" :label="dict.label"
								:value="dict.value" />
						</el-select>
					</el-form-item>
					<el-form-item label="采集频率" prop="dataFrequency">
						<el-input v-model="form.dataFrequency" placeholder="请输入采集频率" />
					</el-form-item>
				</div>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="Probe">
	import {
		ref
	} from "vue"
	import {
		listProbe,
		getProbe,
		delProbe,
		addProbe,
		updateProbe
	} from "@/api/internet/probe"
	import {
		allServeice
	} from "@/api/property/serveice"
	import {
		allDevice
	} from "@/api/property/device"
	import {
		allVirtual
	} from "@/api/property/virtual"
	const {
		proxy
	} = getCurrentInstance()
	const {
		probe_type,
		data_frequency_unit,
		bind_type,
		sys_normal_disable
	} = proxy.useDict("probe_type", "data_frequency_unit", "bind_type", "sys_normal_disable")
	const probeList = ref([])
	const deviceList = ref([])
	const open = ref(false)
	const loading = ref(true)
	const showSearch = ref(true)
	const ids = ref([])
	const single = ref(true)
	const multiple = ref(true)
	const total = ref(0)
	const title = ref("")

	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			deviceName: null,
			probeType: null,
			bindType: null,
			deviceId: null,
			probeType: null,
			port: null,
			dataFrequency: null,
			dataFrequencyUnit: null,
			status: null,
			createdBy: null,
			createdDate: null,
			updatedBy: null,
			updatedDate: null
		},
		rules: {}
	})

	const {
		queryParams,
		form,
		rules
	} = toRefs(data)

	/** 查询网络探针信息列表 */
	function getList() {
		loading.value = true
		listProbe(queryParams.value).then(response => {
			probeList.value = response.rows
			total.value = response.total
			loading.value = false
		})
	}

	function getDeviceInfo(deviceType = 1) { //	获取设备类型
		switch (deviceType) {
			case 1: //	网络设备
				getAllDevice()
				break;
			case 2: //	服务器
				getAllServeice()
				break;
			case 3: //	虚拟机
				getAllVirtual()
				break;
		}
	}

	function getAllServeice() { //	获取服务器信息
		deviceList.value = []
		loading.value = true;
		allServeice().then(response => {
			let {
				code,
				data
			} = response;
			if (code === 200) {
				deviceList.value = data.map(item => ({
					label: item.serverName,
					value: item.serverId
				}));
			}
			loading.value = false;
		});
	}

	function getAllDevice() { //	获取设备信息
		deviceList.value = []
		loading.value = true;
		allDevice({
			deviceType: 1
		}).then(response => {
			let {
				code,
				data
			} = response;
			if (code === 200) {
				deviceList.value = data.map(item => ({
					label: item.deviceName,
					value: item.deviceId
				}));
			}
			loading.value = false;
		});
	}

	function getAllVirtual() { //	获取虚拟机信息
		deviceList.value = []
		loading.value = true;
		allVirtual().then(response => {
			let {
				code,
				data
			} = response;
			if (code === 200) {
				deviceList.value = data.map(item => ({
					label: item.virtualName,
					value: item.virtualId
				}));
			}
			loading.value = false;
		});
	}
	// 取消按钮
	function cancel() {
		open.value = false
		reset()
	}

	function onSelectDevice(event) { //	选择设备信息
		console.log(event, deviceList.value)
		const selectedDevice = deviceList.value.find(device => device.value === event);
		if (selectedDevice) {
			form.value.deviceId = selectedDevice.value
			form.value.deviceName = selectedDevice.label
		}
	}

	// 表单重置
	function reset() {
		form.value = {
			probeId: null,
			bindType: null,
			deviceId: null,
			deviceName: null,
			probeType: null,
			port: null,
			dataFrequency: null,
			dataFrequencyUnit: null,
			status: null,
		}
		proxy.resetForm("probeRef")
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1
		getList()
	}

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef")
		handleQuery()
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.probeId)
		single.value = selection.length != 1
		multiple.value = !selection.length
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset()
		open.value = true
		title.value = "添加网络探针信息"
	}

	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset()
		const _probeId = row.probeId || ids.value
		getProbe(_probeId).then(response => {
			// form.value = response.data
			let editData = response.data
			editData.probeType = editData.probeType+''
			editData.bindType = editData.bindType+''
			editData.dataFrequencyUnit = editData.dataFrequencyUnit+''
			editData.status = editData.status+''
			form.value = editData
			open.value = true
			title.value = "修改网络探针信息"
		})
	}

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["probeRef"].validate(valid => {
			if (valid) {
				if (form.value.probeId != null) {
					updateProbe(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功")
						open.value = false
						getList()
					})
				} else {
					addProbe(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功")
						open.value = false
						getList()
					})
				}
			}
		})
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const _probeIds = row.probeId || ids.value
		proxy.$modal.confirm('是否确认删除网络探针信息编号为"' + _probeIds + '"的数据项？').then(function() {
			return delProbe(_probeIds)
		}).then(() => {
			getList()
			proxy.$modal.msgSuccess("删除成功")
		}).catch(() => {})
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('internet/probe/export', {
			...queryParams.value
		}, `probe_${new Date().getTime()}.xlsx`)
	}

	getList()
</script>